//
//  YWMyReleaseViewController.m
//  百布业务
//
//  Created by 陈炳桦 on 15/10/27.
//  Copyright © 2015年 陈炳桦. All rights reserved.
//  我的发布

#import "YWMyReleaseViewController.h"
#import "YWDemandViewController.h"
#import "YWOrderViewController.h"
#import "YWGroupBuyViewController.h"
#import "YWReleaseDemandViewController.h"

@interface YWMyReleaseViewController () <UIScrollViewDelegate>
/** 标签栏底部的紫色指示器 */
@property (nonatomic, weak) UIView *indicatorView;
/** 当前选中的按钮 */
@property (nonatomic, weak) UIButton *selectedButton;
/** 顶部的所有标签 */
@property (nonatomic, weak) UIView *titlesView;
/** 底部的所有内容 */
@property (nonatomic, weak) UIView *contentView;
@end

@implementation YWMyReleaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 设置导航栏
    [self setupNav];
    
    // 设置顶部的标签栏
    [self setupTitlesView];
    
    // 初始化子控制器
    [self setupChildVC];
    
    // 底部的contentView
    [self setupContentView];
}

// 设置导航栏
- (void)setupNav{
    self.navigationItem.title = @"我的发布";
    self.view.backgroundColor =  BBRGBColor(234, 236, 238);
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发布需求" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItem)];
}

- (void)rightBarButtonItem{
    // 跳转到发布页码的节目
    YWReleaseDemandViewController *releaseVC = [[YWReleaseDemandViewController alloc] init];
    [self.navigationController pushViewController:releaseVC animated:YES];
}

//  设置顶部的标签栏
- (void)setupTitlesView
{
    // 标签栏整体
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [UIColor whiteColor] ;
    titlesView.width = self.view.width;
    titlesView.height = 36;
    [self.view addSubview:titlesView];

    // 底部的指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = BBRGBColor(85, 26, 175);
    indicatorView.height = 2;
    indicatorView.width = BBScreenW / 3;
    indicatorView.tag = -1;
    indicatorView.y = titlesView.height - indicatorView.height;
    [titlesView addSubview:indicatorView];
    self.indicatorView = indicatorView;
    
    // 内部的子标签
    CGFloat width = BBScreenW / 3;
    CGFloat height = titlesView.height;
    NSArray *array = @[@"需求",@"下单",@"集中采购"];
    for (NSInteger i = 0; i<3; i++) {
        UIButton *button = [[UIButton alloc] init];
        button.tag = i;
        button.height = height;
        button.width = width;
        button.x = i * width;

        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:BBRGBColor(85, 26, 175) forState:UIControlStateDisabled];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:button];
        
        // 默认点击了第一个按钮
        if (i == 0) {
            button.enabled = NO;
            self.selectedButton = button;
        }
    }

}

- (void)titleClick:(UIButton *)button{
    // 修改按钮状态
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    self.selectedButton = button;
    
    // 动画
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.x = button.x;
    }];
    
    // 切换
    [self.contentView addSubview:self.childViewControllers[button.tag].view];
   
}

// 初始化子控制器
- (void)setupChildVC{
    YWDemandViewController *VC1 = [[YWDemandViewController alloc] init];
    [self addChildViewController:VC1];
    
    YWOrderViewController *VC2 = [[YWOrderViewController alloc] init];
    [self addChildViewController:VC2];

    
    YWGroupBuyViewController *VC3 = [[YWGroupBuyViewController alloc] init];
    [self addChildViewController:VC3];

}

- (void)setupContentView
{
    // 不要自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIView *contentView = [[UIView alloc] init];
    contentView.frame = CGRectMake(0, 15, BBScreenW, BBScreenH);
    [self.view insertSubview:contentView atIndex:0];
    self.contentView = contentView;
    
    // 添加第一个控制器的view
    [self.contentView addSubview:self.childViewControllers[0].view];

}



@end
